package com.view.resourcemanage.SupplierFrame;

import com.bean.Customer;
import com.bean.Supplier;
import com.bean.User;
import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoTable;
import com.service.SupplierService;
import com.service.impl.SupplierServiceImpl;
import com.util.PageUtilSuper;
import com.view.MainFrame;
import com.view.resourcemanage.CustomerFrame.CustomerAddDialog;
import com.view.resourcemanage.CustomerFrame.CustomerShowDialog;
import com.view.resourcemanage.CustomerFrame.CustomerUpdateDialog;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class SupplierManagePanel extends JPanel {
    private LovoTable supTab;

    private LovoButton supInBtn;
    private LovoButton supChgBtn;
    private LovoButton supInfoBtn;

    private JPanel queryPanel;
    private JLabel nameLab;
    private JLabel rangeLab;
    private JTextField nameTxt;
    private JTextField rangeTxt;
    private JButton queryBtn;

    private MainFrame frame;

    private LovoButton prev,next;

    private JLabel txtPage;
    private PageUtilSuper<Supplier> pageUtilSuper;
    private int pgNumber = 1;
    private int pageSize = 3;

    private SupplierService supplierService = new SupplierServiceImpl();

    public SupplierManagePanel(MainFrame frame) {
        this.frame = frame;
        this.setBackground(Color.WHITE);
        this.setLayout(null);
        setPageUtilSuper();
        this.initTable();
        this.initBtn();
        this.initQueryPanel();
        //this.bindTable();
        txtPage = new JLabel();
        this.txtPage.setBounds(170, 305, 100, 50);
        this.add(this.txtPage);
        labelInit();
    }

    private void labelInit() {
        int maxPage = pageUtilSuper.getMaxNumber();
        this.txtPage.setText(pgNumber+" / "+maxPage);
    }

    public void setPageUtilSuper() {
        List<Supplier> suppliers = new ArrayList<>();
        suppliers = supplierService.getAllSupplier();
        pageUtilSuper = new PageUtilSuper(pageSize,suppliers);
    }

    public void flush(){
        tableFlush();
        labelInit();
    }

    public void tableFlush(){
        setPageUtilSuper();
        List<Supplier> suppliers = new ArrayList<>();
        suppliers = pageUtilSuper.getPgNumberThing(pgNumber);
        supTab.updateLovoTable(suppliers);
    }


    private void initTable() {
        // TODO Auto-generated method stub
        this.supTab = new LovoTable(this, new String[] { "供应商名称", "负责人", "经营范围"
                }, new String[] {"name","director","manageRange"},// 数组中应该是对应属性的属性名
                "id");// 填入唯一标示属性
        List<Supplier> suppliers = new ArrayList<>();
        suppliers = pageUtilSuper.getPgNumberThing(1);
        supTab.updateLovoTable(suppliers);
        this.supTab.setSizeAndLocation(20, 0, 750, 300);

        //bindTable();
    }
    private void initBtn(){
        this.supInBtn = new LovoButton("添加供应商", 30, 380, this);
        this.supChgBtn = new LovoButton("修改供应商信息", 250, 380, this);
        this.supInfoBtn = new LovoButton("查看供应商信息", 30, 450, this);

        this.prev = new LovoButton("上一页",30,320,this);
        this.next = new LovoButton("下一页",250,320,this);

        supInBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                SupplierAddDialog supplierAddDialog = new SupplierAddDialog(frame);
            }
        });

        supChgBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = supTab.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    SupplierUpdateDialog supplierUpdateDialog = new SupplierUpdateDialog(frame,key);
                }
            }
        });

        supInfoBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int key = supTab.getKey();
                if(key == -1){
                    JOptionPane.showMessageDialog(null,"请先选择一行");
                }else {
                    SupplierShowDialog supplierShowDialog = new SupplierShowDialog(frame,key);
                }
            }
        });

        prev.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.havePrev(pgNumber)) {
                    pgNumber--;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是第一页");
                }
            }
        });

        next.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (pageUtilSuper.haveNext(pgNumber)) {
                    pgNumber ++;
                    flush();
                } else {
                    JOptionPane.showMessageDialog(null, "当前是最后一页");
                }
            }
        });

    }

    private void initQueryPanel() {
        // TODO Auto-generated method stub
        this.queryPanel = new JPanel();
        this.queryPanel.setBorder(BorderFactory.createTitledBorder("查询供应商信息"));
        this.queryPanel.setBackground(Color.WHITE);
        this.queryPanel.setLayout(null);
        this.queryPanel.setBounds(380, 310, 300, 180);
        this.add(this.queryPanel);

        // 标签
        this.nameLab = new JLabel("供应商名称");
        this.nameLab.setBounds(35, 30, 60, 30);
        this.queryPanel.add(this.nameLab);
        this.rangeLab = new JLabel("经营范围");
        this.rangeLab.setBounds(40, 70, 50, 30);
        this.queryPanel.add(this.rangeLab);


        // 文本框
        this.nameTxt = new JTextField();
        this.nameTxt.setBounds(100, 37, 130, 20);
        this.queryPanel.add(this.nameTxt);
        this.rangeTxt = new JTextField();
        this.rangeTxt.setBounds(100, 77, 130, 20);
        this.queryPanel.add(this.rangeTxt);


        // 查询按钮
        this.queryBtn = new JButton("查询");
        this.queryBtn.setBounds(200, 140, 80, 20);
        this.queryPanel.add(this.queryBtn);

        queryBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String nameLabText = nameTxt.getText();
                String rangeLabText = rangeTxt.getText();
                List<Supplier> suppliers = new ArrayList<>();
                suppliers = supplierService.findSupplier(nameLabText,rangeLabText);
                supTab.updateLovoTable(suppliers);
            }
        });

    }


}
